<?php
/*本类用于简单包装实现微信高级接口功能*/
class WxService {
  private $appId;
  private $appSecret;
  private $wid;  
 
  public function __construct($wid) { 
    $this->appId = '';
    $this->appSecret = '';  
    $this->wid = $wid;
  }
  /*
  *向指定微信用户发送微信消息
  */
  public function sendTxtMsg($wid,$touser,$msg)
  {
	$apiurl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
	$access_token = $this->getAccessToken();
 
	$url = $apiurl . $access_token;	
	$msgtype="text";

	$params = '{
	        "touser":"'.$touser.'",
	        "msgtype":"text",
	        "text":
	        {
	             "content":"'.$msg.'"
	        }
	    }';
	$res=$this->https_post($url, $params);
 
	$data = json_decode($res, true);
	if($data['errcode'] != 0) {
		throw new RuntimeException($data['errcode'] . ': sendMsg ERR.' . $data['errmsg'], $data['errcode']);
	}
    return $data;	
  }
  function https_post($url,$data)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    if (curl_errno($curl)) {
       return 'Errno'.curl_error($curl);
    }
    curl_close($curl);
    return $result;
}


	//传入appid 和appsecret 
	public function setappid($appid ,$secret){
		$this->appId = $appid ; 
		$this->appSecret = $secret ; 
	}

  private function getAccessToken() {   
       
    return WechatAccount::getAccessToken($this->wid);
  }
  private function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);

    $res = curl_exec($curl);
    curl_close($curl);

    return $res;
  }
}

